﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KnowledgeCommunity.Model;
using KnowledgeCommunity.Web.Models;
using KnowledgeCommunity.Service;

namespace KnowledgeCommunity.Web.DbHelper
{
    public class TopicHelper
    {
        CKCDB mydb = new CKCDB(); 
        /// <summary>
        /// FindAll()
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Topic> FindAll() 
        {
            var apps = from n in mydb.Topics
                      orderby n.AddTime descending
                      select n;
            return apps;
        }
        /// <summary>
        ///  FindTop(int count)
        /// </summary>
        /// <param name="count">数量</param>
        /// <returns></returns>
        public IEnumerable<Topic> FindTop(int count)
        {
            var apps = (from n in mydb.Topics
                        where n.BelongTo == 1
                        orderby n.ClickNum, n.AddTime descending
                        select n).Take(count);
            return apps;
        }
        /// <summary>
        /// FindById(int id)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Topic FindById(int id) 
        {
            var app = mydb.Topics.Find(id);
            return app;
        }

        /// <summary>
        ///  FindByBelongTo(int belong)
        /// </summary>
        /// <param name="belong">新闻类型：1-Topic;2-about;3-contact;4-Q&A;5-cooperating(合作机构);6-terms(使用条款);7-user guide(使用指南)</param>
        /// <returns></returns>
        public IEnumerable<Topic> FindByBelongTo(int belong)
        {
            var app = from n in mydb.Topics
                      where n.BelongTo == belong
                      orderby n.AddTime descending
                      select n;
            return app;
        }

        /// <summary>
        /// FindByType(string type) 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public IEnumerable<Topic> FindByType(string type) 
        {
            var app = from n in mydb.Topics
                       where n.FirstType == type
                       orderby n.AddTime descending
                       select n;
            return app;
        }

        /// <summary>
        /// SearchByKeyAndTime
        /// </summary>
        /// <param name="key">关键字</param>
        /// <param name="stime">起始时间</param>
        /// <param name="etime">结束时间</param>
        /// <returns></returns>
        public IEnumerable<Topic> SearchByKeyAndTime(string key, string cont, DateTime stime, DateTime etime)
        {
            IEnumerable<Topic> apps = null;
            DateTime start = Convert.ToDateTime("2012-1-2");
            DateTime end = Convert.ToDateTime("2100-12-30");

            if (key == "" && cont == "" && stime < start && etime > end)   //00<>
            {
                apps = FindAll();
            }
            else if (key == "" && cont == "" && stime < start && etime > end)   //00>>
            {
                apps = from n in mydb.Topics
                       where n.AddTime >= Convert.ToDateTime(stime)
                       orderby n.AddTime descending
                       select n;
            }
            else if (key == "" && cont == "" && stime < start && etime > end)   //00<<
            {
                apps = from n in mydb.Topics
                       where n.AddTime <= Convert.ToDateTime(etime)
                       orderby n.AddTime descending
                       select n;
            }
            else if (key == null && cont == null && stime < start && etime > end)   //00><
            {
                apps = from n in mydb.Topics
                       where n.AddTime >= Convert.ToDateTime(stime) && n.AddTime <= Convert.ToDateTime(etime)
                       orderby n.AddTime descending
                       select n;
            }
            else if (key != "" && cont == "" && stime < start && etime > end)   //10<>
            {
                apps = from n in mydb.Topics
                       where n.Title.Contains(key)
                       orderby n.AddTime descending
                       select n;
            }
            else if (key != "" && cont == "" && stime < start && etime > end)   //10>>
            {
                apps = from n in mydb.Topics
                       where n.Title.Contains(key) && n.AddTime >= Convert.ToDateTime(stime)
                       orderby n.AddTime descending
                       select n;
            }
            else if (key != "" && cont == "" && stime < start && etime > end)   //10<<
            {
                apps = from n in mydb.Topics
                       where n.Title.Contains(key) && n.AddTime <= Convert.ToDateTime(etime)
                       orderby n.AddTime descending
                       select n;
            }
            else if (key != "" && cont == "" && stime < start && etime > end)   //10><
            {
                apps = from n in mydb.Topics
                       where n.Title.Contains(key) && n.AddTime >= Convert.ToDateTime(stime) && n.AddTime <= Convert.ToDateTime(etime)
                       orderby n.AddTime descending
                       select n;
            }
            else if (key == "" && cont != "" && stime < start && etime > end)   //01<>
            {
                apps = from n in mydb.Topics
                       where n.Content.Contains(cont)
                       orderby n.AddTime descending
                       select n;
            }
            else if (key == "" && cont != "" && stime > start && etime > end)   //01>>
            {
                apps = from n in mydb.Topics
                       where n.Content.Contains(cont) && n.AddTime >= Convert.ToDateTime(stime)
                       orderby n.AddTime descending
                       select n;
            }
            else if (key == "" && cont != "" && stime < start && etime < end)   //01<<
            {
                apps = from n in mydb.Topics
                       where n.Content.Contains(cont) && n.AddTime <= Convert.ToDateTime(etime)
                       orderby n.AddTime descending
                       select n;
            }
            else if (key == "" && cont != "" && stime > start && etime < end)   //01><
            {
                apps = from n in mydb.Topics
                       where n.Content.Contains(cont) && n.AddTime >= Convert.ToDateTime(stime) && n.AddTime <= Convert.ToDateTime(etime)
                       orderby n.AddTime descending
                       select n;
            }
            return apps;
        }

        /// <summary>
        /// return 0-Fail; 1-Success
        /// </summary>
        /// <param name="model">Topic</param>
        /// <returns></returns>
        public bool Create(Topic model) 
        {
            try
            {
                mydb.Topics.Add(model);
                mydb.SaveChanges();
                return true;
            }
            catch (Exception ex) 
            {
                string message = "Create in table<Topics>" + ex.ToString();
                WriteLog.WriteSysLog(message);
                return false;
            }
        }

        /// <summary>
        /// return 0-Fail; 1-Success
        /// </summary>
        /// <param name="model">Topic</param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            var model = FindById(id);
            try
            {
                mydb.Topics.Remove(model);
                mydb.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                string message = "Delete in table<Topics>" + ex.ToString();
                WriteLog.WriteSysLog(message);
                return false;
            }
        }

        


    }
}